//
//  SiJiCell.m
//  Created 阿帕 on 2019/12/20.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "SiJiManageCell.h"

@interface SiJiManageCell ()
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *chexingLabel;
@property (weak, nonatomic) IBOutlet UILabel *chepaiLabel;
@property (weak, nonatomic) IBOutlet UILabel *carInfoLabel;
@property (weak, nonatomic) IBOutlet UIView *line;
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;
@property (nonatomic, strong) GQRatingView *ratingView;
@property (nonatomic, strong) UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UIView *buttonBgView;

@end

@implementation SiJiManageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    cut(_headImageView, _headImageView.cy_width/2);
    _nicknameLabel.textColor = Dark_TextColor;
    _chexingLabel.textColor = Light_TextColor;
    _chepaiLabel.textColor = Light_TextColor;
    _carInfoLabel.textColor = Light_TextColor;
    _line.backgroundColor = BackgroundColor;
    
    _scoreLabel = [[UILabel alloc] init];
    _scoreLabel.textColor = Light_TextColor;
    _scoreLabel.font = font(13);
    _carInfoLabel.numberOfLines = 2;
    [self.contentView addSubview:_scoreLabel];
    
    //五角星 链式调用 [NSNumber numberWithInt:_model.avgBasicGrade]
    if (!_ratingView) {
        _ratingView = [GQRatingView init]
        .frameChain(CGPointMake(_headImageView.cy_right + 15, _nicknameLabel.cy_origin.y ),15)
        .canTouchChain(NO)
        .needIntValueChain(YES)
        .scoreNumChain([NSNumber numberWithFloat:_model.avgBasicGrade])
        .normalColorChain([UIColor cy_colorWithHexString:@"#C0CDDE"])
        .highlightColorChian([UIColor cy_colorWithHexString:@"#FFC000"])
        .scroreBlockChain(^(NSNumber *scoreNumber){
            
        }).superViewChain(self);
    }
    
    
    NSArray *titleArr = @[@"审核状态", @"证件是否到期", @"载货状态", @"承运吨位", @"代收协议状态"];
    
    CGFloat bgY = 60;
    for (int i=0; i<titleArr.count; i++) {
        
        CGFloat bgH = i==
        0?50:30;
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, bgY, ScreenW, bgH)];
        bgY += bgH;
        [self.contentView addSubview:bgView];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, bgView.height)];
        textLabel.tag = 2000 + i;
        textLabel.text = titleArr[i];
        textLabel.textAlignment = NSTextAlignmentLeft;
        textLabel.textColor = Light_TextColor;
        textLabel.font = font(13);
        textLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:textLabel];
        
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, ScreenW-115, bgView.height)];
        valueLabel.tag = 200 + i;
        valueLabel.text = @"";
        valueLabel.textAlignment = NSTextAlignmentRight;
        valueLabel.textColor = Light_TextColor;
        valueLabel.font = font(13);
        valueLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:valueLabel];
        
    }
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 45)];
    scrollView.tag = 1000;
    [_buttonBgView addSubview:scrollView];
}

- (void)buttonClick:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellButtonIndexClick:withButton:)]) {
        [self.delegate cellButtonIndexClick:button.tag withButton:button];
    }
}

- (void)setModel:(SiJiListModel *)model {
    
    _model = model;
    
    /*
     0-独立司机,1-车队,2-车队司机,3-独立司机(不包含加入车队的独立司机)、车队，4-独立司机、车队司机，5-车队、车队司机，6-独立司机、车队
     */
    
    UIScrollView *scrollView = [self.contentView viewWithTag:1000];
    
    CGFloat btnWidth = ScreenW/4;
    
    if (scrollView) {
        //解决按钮重用问题
        [scrollView removeAllSubviews];
    }
    
    NSMutableArray *btnTitles = [[NSMutableArray alloc] init];
    NSMutableArray *btnImages = [[NSMutableArray alloc] init];
    
    NSString *xiYiState = @"";
    if (_model.collection == 1) {
        xiYiState = @"已同意";
        UILabel *label = [self viewWithTag:202];
        label.textColor = Light_TextColor;
        
        [btnTitles addObject:@"查看协议"];
        [btnImages addObject:@"gcy_icon_my_daishouxieyi"];
    } else {
        xiYiState = @"未授权或超出协议授权时间";
        UILabel *label = [self viewWithTag:202];
        label.textColor = Red_TextColor;
    }
    
    [btnTitles addObject:@"绑定车辆"];
    [btnTitles addObject:@"冻结"];
    [btnTitles addObject:@"编辑"];
    [btnTitles addObject:@"移出车队"];
    
    [btnImages addObject:@"gcy_icon_sjgl_bindcar"];
    [btnImages addObject:@"gcy_icon_sjgl_dongjie"];
    [btnImages addObject:@"gcy_icon_yundan_edit"];
    [btnImages addObject:@"gcy_icon_yundan_delete"];
    
    scrollView.contentSize = CGSizeMake(ScreenW/4 * btnTitles.count, 45);
    
    // 0独立司机
    if (model.driverType == 0) {
        
        [btnTitles removeAllObjects];
        [btnImages removeAllObjects];
        
        if (_model.collection == 1) {
            [btnTitles addObject:@"查看协议"];
            [btnImages addObject:@"gcy_icon_my_daishouxieyi"];
        }
        
        [btnTitles addObject:@"移出车队"];
        [btnImages addObject:@"gcy_icon_yundan_delete"];
    }
    
    for (int i=0; i<btnTitles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 100 + i;
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitle:btnTitles[i] forState:UIControlStateNormal];
        button.titleLabel.font = font(14);
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        [button setTitleColor:Dark_TextColor forState:UIControlStateNormal];
        [button setImage:image(btnImages[i]) forState:UIControlStateNormal];
        button.frame = CGRectMake(btnWidth * i, 0, btnWidth, 45);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [scrollView addSubview:button];
        
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    }
    
    //操作按钮改变
    UIButton *bindBtn = [self viewWithTag:101];
    UIButton *dongBtn = [self viewWithTag:102];
    
    //    bindStatus (integer, optional): 绑定车辆 1未绑定，2已绑定 ,
    if (model.driverType == 2) {
        if ([CJStringWithFormat(@"%ld",(long)model.bindStatus) isEqual:@"1"]) {
            //未绑定
            [bindBtn setTitle:@"绑定车辆" forState:UIControlStateNormal];
        } else {
            [bindBtn setTitle:@"解绑车辆" forState:UIControlStateNormal];
        }
    }
    
    //是否冻结（0-未冻结 1-已冻结）
    if ([CJStringWithFormat(@"%ld",(long)model.isFreeze) isEqual:@"0"]) {
        //未绑定
        [dongBtn setTitle:@"冻结" forState:UIControlStateNormal];
    } else {
        [dongBtn setTitle:@"解冻" forState:UIControlStateNormal];
    }
    
    setNetImage(_headImageView, _model.headImg, @"gcy_img_siji_dHead");
    _nicknameLabel.text = _model.name;
    _scoreLabel.text = CJStringWithFormat(@"%.1f",_model.avgBasicGrade);
    _chexingLabel.text = [NSString stringWithFormat:@"车型：%@", [NSString nullToString:CJStringWithFormat(@"%@", _model.vehicleClassificationName)]];
    _chepaiLabel.text = _model.vehicleLicenseNumber;
    
    //    _carInfoLabel.text = [NSString stringWithFormat:@"状态：%@ | 吨位：%@ | 代收协议状态：%@",_model.vehicleLoadStatusName, [NSString nullToString:CJStringWithFormat(@"%@", _model.vehicleLoadWeightName)], xiYiState];
    
    NSArray *arr = @[@"",@"",
                     [NSString nullToString:_model.vehicleLoadStatusName],
                     [NSString nullToString:_model.vehicleLoadWeightName],
                     xiYiState,
    ];
    
    for (int i=0; i<arr.count; i++) {
        UILabel *label = [self.contentView viewWithTag:200+i];
        UILabel *textLabel = [self.contentView viewWithTag:2000+i];
        if (i==0) {
            [StatusTextJudge authStatusJudge:_model.authStatusExt authMsg:_model.authMsg label:label];
        } else if (i==1) {
            label.textColor = _model.driverCertificateHasDue == 0 ? Light_TextColor : [UIColor redColor];
            label.text = _model.driverCertificateHasDue == 0 ? @"未到期" : @"已到期";
            
//            label.hidden = _model.driverCertificateHasDue == 0 ? YES : NO;
//            textLabel.hidden = _model.driverCertificateHasDue == 0 ? YES : NO;
        } else {
            label.text = arr[i];
        }
    }
    
    CGSize size = [DCSpeedy dc_calculateTextSizeWithText:_model.name WithTextFont:14 WithMaxW:500];
    
    _ratingView.cy_x = _headImageView.cy_right + 15 + size.width;
    _scoreLabel.frame = CGRectMake(_headImageView.cy_right + 15 + size.width + 80, _nicknameLabel.cy_y, 60, 20);
    _scoreLabel.cy_centerY = _nicknameLabel.cy_centerY;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
